package com.hankol.newretail

import android.graphics.Typeface
import android.os.Bundle
import android.util.TypedValue
import android.view.Gravity
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.Constants
import com.baselibrary.extension.checkNotNull
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ARouterUtils
import com.google.android.material.tabs.TabLayout
import com.baselibrary.activity.BaseActivity
import com.hankol.newretail.adapter.order.MyOrderTabVp
import com.hankol.newretail.databinding.ActivityMyOrderBinding
import com.hankol.newretail.fragment.MyOrderFragment
import com.hankol.newretail.viewmodel.OrderAboutViewModel

/**新零售-我的订单
 * @author: merbng
 * @time: 2022/8/23 12:53
 * @description:
 */
@Route(path = ActivityRouter.NEW_RETAIL.MY_ORDER)
class MyOrderActivity : BaseActivity() {
    private lateinit var dataBinding: ActivityMyOrderBinding
    private lateinit var viewModel: OrderAboutViewModel
    private var statusType = 0
    private var tabList = mutableListOf<String>("待付款", "待发货", "待收货", "已完成", "全部")

    private val pagerAdapter: MyOrderTabVp by lazy { MyOrderTabVp(supportFragmentManager) }

    private var fragmentList: ArrayList<MyOrderFragment> = arrayListOf(//状态:-1-全部，0-待付款，1-待发货，2-待收货）,5-已完成
            MyOrderFragment.newInstance(0), MyOrderFragment.newInstance(1), MyOrderFragment.newInstance(2), MyOrderFragment.newInstance(5), MyOrderFragment.newInstance(-1))

    override fun getLayoutId(): Int {
        initImmersion(true)
        return R.layout.activity_my_order
    }

    override fun init() {
        if (ARouterUtils.parseParams(intent.extras!!, Constants.KEY_ORDER_TYPE)[0].checkNotNull()) {
            statusType = ARouterUtils.parseParams(intent.extras!!, Constants.KEY_ORDER_TYPE)[0].toInt()
        }
        setStatusBarHeight(R.color.color_F9F9F9)
        dataBinding = getDataBinding<ActivityMyOrderBinding>()
        viewModel = getActivityViewModelProvider(this).get(OrderAboutViewModel::class.java)
        dataBinding.imgBack.setOnClickListener { finish() }
        dataBinding.tvOpenSearch.setOnClickListener {
            val bundle = Bundle()
            bundle.putBoolean(Constants.KEY_SEARCH_ORDER_IS_MANAGER, false)
            ARouterUtils.jump(ActivityRouter.WINE_CELLAR.SEARCH_ORDER, bundle)
        }
        pagerAdapter.setTitleList(tabList)
        pagerAdapter.setDatas(fragmentList)
        dataBinding.vpMyOrder.adapter = pagerAdapter
        dataBinding.vpMyOrder.offscreenPageLimit = tabList.size
        dataBinding.tabLayout.setupWithViewPager(dataBinding.vpMyOrder)
        dataBinding.tabLayout.addOnTabSelectedListener(getTabSelectedListener())
        dataBinding.vpMyOrder.currentItem = statusType
    }

    private fun getTabSelectedListener() = object : TabLayout.OnTabSelectedListener {
        override fun onTabSelected(tab: TabLayout.Tab?) {
            //Tab选中的样式
            setTab(tab)
        }

        override fun onTabUnselected(tab: TabLayout.Tab?) {         //非选中时的样式
            tab?.customView = null
        }

        override fun onTabReselected(p0: TabLayout.Tab?) {}
    }

    fun setTab(tab: TabLayout.Tab?) {
        val textView = TextView(this)
        //字体样式
        val selectedSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 16f, resources.displayMetrics)
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, selectedSize)
        textView.typeface = Typeface.defaultFromStyle(Typeface.BOLD) //加粗
        textView.gravity = Gravity.CENTER
        // 选中的字体颜色
        textView.setTextColor(ContextCompat.getColor(this, R.color.color_D53))
        textView.text = tab!!.text
        tab.customView = textView
    }
}